/*
 * Copyright (c) 2011 Technology Ltd.
 * All rights reserved.
 * project: nxcrm
 * create: 2011-3-21
 * cvs: $Id: DateUtil.java,v 1.1 2012/01/05 08:22:47 lawever Exp $
 */
package cn.batch.daemon.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * 日期处理
 * 
 * @author xihn
 * @version $Revision: 1.1 $
 */
public class DateUtil {
	
	/**
	 * 得到本月的最后一天的前一天 如当前为10月31,返回10月30
	 */

	public static Date getMonthLastDayDecreaseOneDay() {
		Date lastDayDecreaseOneDay = decreaseOneDay(getMonthLastDay());		
		return lastDayDecreaseOneDay;
	}

	/**
	 * 得到本月的最后一天
	 */

	public static Date getMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	/**
	 * 日期加一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date addOneDay(Date date) {
		return addMoreDay(date, 1);
	}

	/**
	 * 日期增加n天
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addMoreDay(Date date, int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dateOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, dateOfMonth + n);
		return calendar.getTime();
	}

	/**
	 * 日期减一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date decreaseOneDay(Date date) {
		return decreaseMoreDay(date, 1);
	}

	/**
	 * 日期减少n天
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date decreaseMoreDay(Date date, int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dateOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, dateOfMonth - n);
		return calendar.getTime();
	}
	
	/** 得到一天的开始日期时间 */
	public static Date startTimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/** 得到一天的结束日期时间 */
	public static Date endTimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 转换 Date 对象为日期字符串。
	 * @param date
	 * @param format 日期字符串的格式
	 * @return string
	 */
	public static String currDayData(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 转换日期字符串为 Date 对象。
	 * @param dateStr 日期字符串
	 * @param format 日期字符串的格式
	 * @return
	 * @throws ParseException
	 */
	public static Date fromString(String dateStr, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(dateStr);
	}

	/**
	 * 转换日期字符串，从源格式到指定格式。
	 * @param dateStr 日期字符串
	 * @param fromFormat 源格式
	 * @param toFormat 目标格式
	 * @return 目标格式字符串
	 * @throws ParseException
	 */
	public static String zhDateString(String dateStr, String fromFormat,
			String toFormat) throws ParseException {
		Date date = fromString(dateStr, fromFormat);
		return currDayData(date, toFormat);
	}

	/**
	 * 将月份转换成当月月初和月末日期。 如 2012-09 转换成 [2012-09-01, 2012-09-30]
	 * 
	 * @param args
	 */
	public static String[] convertMonth(String monthString) {
		if (StringUtils.isBlank(monthString)) {
			return null;
		}
		String[] temp = monthString.split("-");

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(temp[0]));
		c.set(Calendar.MONTH, Integer.parseInt(temp[1]) - 1);

		String[] result = new String[2];
		result[0] = monthString + "-"
				+ c.getActualMinimum(Calendar.DAY_OF_MONTH);
		result[1] = monthString + "-"
				+ c.getActualMaximum(Calendar.DAY_OF_MONTH);
		return result;
	}

	/**
	 * 将月份转换成上个月的月初和月末日期。 如 2012-09 转换成 [2012-08-01, 2012-08-31]
	 * 
	 * @param args
	 */
	public static String[] convertLastMonth(String monthString) {
		if (StringUtils.isBlank(monthString)) {
			return null;
		}
		String[] temp = monthString.split("-");

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(temp[0]));
		c.set(Calendar.MONTH, Integer.parseInt(temp[1]) - 1);
		c.add(Calendar.MONTH, -1); // 上个月

		monthString = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1);

		String[] result = new String[2];
		result[0] = monthString + "-"
				+ c.getActualMinimum(Calendar.DAY_OF_MONTH);
		result[1] = monthString + "-"
				+ c.getActualMaximum(Calendar.DAY_OF_MONTH);
		return result;
	}

	/**
	 * 将季度转换成季度初和季度末的日期。 如 Q1 > [01-01, 03-31]、 Q2 > [04-01, 06-30]、 Q3 >
	 * [07-01, 09-30]、 Q4 > [10-01, 12-31]
	 * 
	 * @param year
	 * @param quarter
	 * @return
	 */
	public static String[] convertQuarter(String year, String quarter) {
		if (StringUtils.isBlank(year) || StringUtils.isBlank(quarter)) {
			return null;
		}
		String[] result = new String[2];
		if ("Q1".equalsIgnoreCase(quarter)) {
			result[0] = year + "-01-01";
			result[1] = year + "-03-31";
		} else if ("Q2".equalsIgnoreCase(quarter)) {
			result[0] = year + "-04-01";
			result[1] = year + "-06-30";
		} else if ("Q3".equalsIgnoreCase(quarter)) {
			result[0] = year + "-07-01";
			result[1] = year + "-09-30";
		} else if ("Q4".equalsIgnoreCase(quarter)) {
			result[0] = year + "-10-01";
			result[1] = year + "-12-31";
		}
		return result;
	}

	/**
	 * 将季度转换成上个季度初和季度末的日期。 如 Q1 > [01-01, 03-31]、 Q2 > [04-01, 06-30]、 Q3 >
	 * [07-01, 09-30]、 Q4 > [10-01, 12-31]
	 * 
	 * @param year
	 * @param quarter
	 * @return
	 */
	public static String[] convertLastQuarter(String year, String quarter) {
		if (StringUtils.isBlank(year) || StringUtils.isBlank(quarter)) {
			return null;
		}
		String[] result = new String[2];
		if ("Q1".equalsIgnoreCase(quarter)) {
			result[0] = (Integer.parseInt(year) - 1) + "-10-01";
			result[1] = (Integer.parseInt(year) - 1) + "-12-31";

		} else if ("Q2".equalsIgnoreCase(quarter)) {
			result[0] = year + "-01-01";
			result[1] = year + "-03-31";

		} else if ("Q3".equalsIgnoreCase(quarter)) {
			result[0] = year + "-04-01";
			result[1] = year + "-06-30";

		} else if ("Q4".equalsIgnoreCase(quarter)) {
			result[0] = year + "-07-01";
			result[1] = year + "-09-30";
		}
		return result;
	}
	
	/**
	* 计算两个日期之间相差的月数
	* 
	* @param date1
	* @param date2
	* @return
	*/
	public static int getMonths(Date date1, Date date2) {
		   int iMonth = 0;
//		   int flag = 0;
		   try {
		    Calendar objCalendarDate1 = Calendar.getInstance();
		    objCalendarDate1.setTime(date1);

		    Calendar objCalendarDate2 = Calendar.getInstance();
		    objCalendarDate2.setTime(date2);

		    if (objCalendarDate2.equals(objCalendarDate1))
		     return 0;
		    if (objCalendarDate1.after(objCalendarDate2)) {
		     Calendar temp = objCalendarDate1;
		     objCalendarDate1 = objCalendarDate2;
		     objCalendarDate2 = temp;
		    }
//		    if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1
//		      .get(Calendar.DAY_OF_MONTH))
//		     flag = 1;

		    if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1
		      .get(Calendar.YEAR))
		     iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1
		       .get(Calendar.YEAR))
		       //* 12 + objCalendarDate2.get(Calendar.MONTH) - flag)
		       * 12 + objCalendarDate2.get(Calendar.MONTH))
		       - objCalendarDate1.get(Calendar.MONTH);
		    else
		     iMonth = objCalendarDate2.get(Calendar.MONTH)
//		       - objCalendarDate1.get(Calendar.MONTH) - flag;
		       - objCalendarDate1.get(Calendar.MONTH);

		   } catch (Exception e) {
		    e.printStackTrace();
		   }
		   return iMonth;
		} 

	public static void main(String args[]) throws Exception {

		// @SuppressWarnings("unused")
		// String[] s = convertLastMonth("2010-01") ;

		//@SuppressWarnings("unused")
		//String[] q = convertLastQuarter("2012", "Q1");

		/*
		 * Calendar calendar = Calendar.getInstance();
		 * calendar.set(Calendar.YEAR, 2011); calendar.set(Calendar.MONTH, 1);
		 * 
		 * System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		 * System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		 */

		// System.out.println(calendar.getTime().toLocaleString());
		
		Date date = fromString("20121130", "yyyyMMdd");
		date = addOneDay(date);
		System.out.println(date.toString());
	}
}
